package top.iridescent.util;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Copyright (c) 2006, 江苏新大陆  All rights reserved
 *
 * @author hexuey
 * @createDate 2006-5-8 16:50:38
 * @version 1.0
 */
public class StringTools {
	/**
	 * 数字式的显示格式
	 */
	public static NumberFormat NUM_FMT_INT_FLT = NumberFormat.getNumberInstance();
	public static NumberFormat NUM_FMT_PER = NumberFormat.getPercentInstance();
	public static Pattern INTEGER = Pattern.compile("\\d*");
	public static Pattern NUMBER = Pattern.compile("^\\d+[.\\d]?\\d*$");
	/**
	 * @see String#valueOf(Object)
	 */
	public static String valueOf(Object obj) {
		return (obj == null) ? "" : obj.toString();
	}
	/**
	 * 检查对象是否为空，如果为空就返回空字符串
	 * @param strValue
	 * @return 如果为空返回“”，否则返回strValue;
	 */
	public static String notNull(String strValue) {
		return notNull(strValue, "");
	}
	/**
	 * 检查对象是否为空，如果为空就返回指定默认的值
	 * 
	 * @param strValue 检查字符串对象
	 * @param strDefault 默认值
	 * @return 返回相应值
	 */
	public static String notNull(String strValue, String strDefault) {
		if (strValue == null) {
			return strDefault;
		}
		return strValue;
	}
	/**
	 * 将String转换为整形
	 *
	 * @param inputNumber
	 * @param defaultNum
	 * @return int
	 */
	public static int parseInt(String inputNumber, int defaultNum) {
		if (inputNumber == null){
			return defaultNum;
		}
		int temp = defaultNum;
		try {
			temp = Integer.parseInt(inputNumber.trim());
		} catch (Exception ex) {
			temp = defaultNum;
		}
		return temp;
	}
	/**
	 * 将String转换为Double
	 */
	public static double parseDouble(String value, double defaultNum) {
		if (value == null || "".equals(value)) {
			return defaultNum;
		}
		double temp = defaultNum;
		try {
			temp = Double.parseDouble(value);
		} catch (Exception ex) {
			temp = defaultNum;
		}
		return temp;
	}
	/**
	 * 将String转换为Long
	 */
	public static long parseLong(String value, long defaultNum) {
		if (value == null || "".equals(value)) {
			return defaultNum;
		}
		long temp = defaultNum;
		try {
			temp = Long.parseLong(value);
		} catch (Exception ex) {
			temp = defaultNum;
		}
		return temp;
	}
	/**
	 * 把字符串数组组合成以split为分隔的字符串
	 * 
	 * @param args 字符串数组
	 * @param split 分隔字符串
	 * @return 返回组合后的字符串
	 */
	public static String toString(String[] args, String split) {
		if (args == null){
			return "";
		}
		String strRet = "";
		for (int i = 0; i < args.length; i++) {
			if (i == 0) {
				strRet = args[i];
			} else {
				strRet += split + args[i];
			}
		}
		return strRet;
	}
	public static String toString(Object[] args, String split) {
		if (args == null){
			return "";
		}
		String strRet = "";
		for (int i = 0; i < args.length; i++) {
			if (i == 0) {
				strRet = valueOf(args[i]);
			} else {
				strRet += split + args[i];
			}
		}
		return strRet;
	}
	public static String toString(int[] args, String split) {
		if (args == null){
			return "";
		}
		String strRet = "";
		for (int i = 0; i < args.length; i++) {
			if (i == 0) {
				strRet = String.valueOf(args[i]);
			} else {
				strRet += split + args[i];
			}
		}
		return strRet;
	}
	public static String toString(long[] args, String split) {
		if (args == null){
			return "";
		}
		String strRet = "";
		for (int i = 0; i < args.length; i++) {
			if (i == 0) {
				strRet = String.valueOf(args[i]);
			} else {
				strRet += split + args[i];
			}
		}
		return strRet;
	}
	public static String toString(double[] args, String split) {
		if (args == null){
			return "";
		}
		String strRet = "";
		for (int i = 0; i < args.length; i++) {
			if (i == 0) {
				strRet = String.valueOf(args[i]);
			} else {
				strRet += split + args[i];
			}
		}
		return strRet;
	}
	public static String toString(char[] args, String split) {
		if (args == null){
			return "";
		}
		String strRet = "";
		for (int i = 0; i < args.length; i++) {
			if (i == 0) {
				strRet = String.valueOf(args[i]);
			} else {
				strRet += split + args[i];
			}
		}
		return strRet;
	}
	/**
	 * 解析出的字符串对象中符合规则的所有对象
	 * 如:/test.do?id=$id$&name=$NAME1$&name=$name2$&name=$name3$
	 * 解析出:id,NAME1,name2,name3
	 * 
	 * @param parseValue 要解析的字符串对象
	 * @param p 要查询的字符串规则表达式
	 * @return 返回查询到的所有符合规则的字符串对象
	 */
	public static List parseString(String parseValue, Pattern p) {
		List lisRet = new ArrayList();
		Matcher m = p.matcher(parseValue);
		while (m.find()) {
			lisRet.add(m.group(1));
		}
		return lisRet;
	}
	/**
	 * 把传入的<code>value</code>格式成百分率的格式，同时保留2位小数（4舍5入）
	 * @param value 格式数值
	 * @return 返回数值的百分率格式,如果不是数字,则返回原数据
	 */
	public static String toPercentString(String value) {
		return toPercentString(value, 2, 2);
	}
	public static String toPercentString(String value, int maxFraction, int minFraction) {
		try {
			double dValue = Double.parseDouble(value);
			NUM_FMT_PER.setMaximumFractionDigits(maxFraction);
			NUM_FMT_PER.setMinimumFractionDigits(minFraction);
			value = NUM_FMT_PER.format(dValue);
		} catch (Exception e) {
			return value;
		}
		return value;
	}
	/**
	 * 格式化数字显示格式
	 * @param formatNum 当前格式化的数字
	 * @param maxDigit 格式返回的小数位数
	 * @return 返回格式化后的数字
	 */
	public static String format(double formatNum, int maxDigit) {
		String value = String.valueOf(formatNum);
		try {
			NUM_FMT_INT_FLT.setMinimumFractionDigits(maxDigit);
			NUM_FMT_INT_FLT.setMaximumFractionDigits(maxDigit);
			value = NUM_FMT_INT_FLT.format(formatNum);
		} catch (Exception e) {
			return value;
		}
		return value;
	}
	public static String format(String formatNum, int maxDigit) {
		if (formatNum == null || formatNum.length() <= 0){
			return formatNum;
		}
		return format(parseDouble(formatNum, 0), maxDigit);
	}
	/**
	 * 检查输入的字符串是否是全数字（整数）
	 * @param str 输入字符串
	 * @return true：是；false：否
	 */
	public static boolean isInteger(String str) {
		return INTEGER.matcher(str).matches();
	}
	/**
	 * 检查输入的字符串是否是全数字（小数）
	 * @param str 输入字符串
	 * @return true：是；false：否
	 */
	public static boolean isNumber(String str) {
		return NUMBER.matcher(str).matches();
	}
	/**
	 * 
	*  Description：解析“，”分隔的字符串，每个字符串加上“‘”
	*  date:2012-6-12 - 上午10:56:03
	*  @author cc
	*  @tags @param str
	*  @tags @return
	 */
	public static String strToOracleStr(String str) {
		try {
			String[] strArr = str.split(",");
			StringBuffer newStr = new StringBuffer();
			for (int i = 0; i < strArr.length; i++) {
				if (i > 0){
					newStr.append(",");
				}
				newStr.append("'").append(strArr[i]).append("'");
			}
			return newStr.toString();
		} catch (Exception e) {
			System.err.println("无法解析的数据格式：[" + str + "]");
			return "";
		}
	}
	/**
	 * 
	*  Description：解析“，”分隔的字符串，每个字符串加上“‘”
	*  date:2012-6-12 - 上午10:56:03
	*  @author cc
	*  @tags @param str
	*  @tags @return
	 */
	public static String toOracleStr(List<String> idList, boolean isString) {
		if (idList == null || idList.size() <= 0){
			return "";
		}
		StringBuffer newStr = new StringBuffer();
		for (int i = 0; i < idList.size(); i++) {
			String id = idList.get(i);
			if (id == null || "".equals(id)){
				continue;
			} else {
				if (isString){
					newStr.append("'").append(id).append("',");
				} else{
					newStr.append(id + ",");
				}
			}
		}
		if (newStr.length() != 0) {
			return newStr.toString().substring(0, newStr.length() - 1);
		}
		return "";
	}
	/**
	 * 将List的id拆解成1,2,3
	 * <一句话功能简述>
	 * <功能详细描述>
	 * @param idList
	 * @param splitKey
	 * @param isString
	 * @return [参数说明]
	 * 
	 * @return String [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public static String splitListToString(List<String> idList, String splitKey, boolean isString) {
		if (StringUtils.isEmpty(splitKey)) {
			return "";
		}
		if (null != idList && idList.size() > 0) {
			StringBuffer sb = new StringBuffer();
			for (String id : idList) {
				if (!StringUtils.isEmpty(id)) {
					if (!isString) {
						sb.append(id).append(splitKey);
					} else {
						sb.append("'").append(id).append("'").append(splitKey);
					}
				}
			}
			if (sb.length() != 0) {
				return sb.toString().substring(0, sb.length() - 1);
			}
		}
		return "";
	}
	/**
	 * 
	*  Description：
	*  date:2014-2-20 - 上午9:44:20
	*  @author cc
	*  @tags @param ids
	*  @tags @return
	 */
	public static List<String> formatList(String[] ids) {
		if (ids == null || ids.length <= 0){
			return new ArrayList<String>();
		}
		List<String> dataList = new ArrayList<String>();
		for (String id : ids) {
			dataList.add(id);
		}
		return dataList;
	}
	/**
	 * 
	*  Description：
	*  date:2013-4-10 - 下午4:13:35
	*  @author cc
	*  @tags @param str
	*  @tags @return
	 */
	public static String trim(String str) {
		if (str == null){
			return "";
		} else{
			return str.trim();
		}
	}
	/**
	 * 将字符串按照分割字符串转成list
	 *  "S0001,S0002,S0003"字符串转成list
	 * @author wjjNL
	 * @param str
	 * @param patton
	 * @return
	 * @throws Exception
	 */
	public static List<String> splitStringToList( String str,String patton){
		List<String> list =new ArrayList<String>();
		if(true){
			if(str.contains(patton)){
				String[] strs=str.split(patton);
				for(int i = 0; i < strs.length; i++){
				    list.add(strs[i]);
				}
			}else{
				list.add(str);
			}
		}
		return list;
	}
	/**
	 * main method
	 * @param args
	 */
	public static void main(String[] args) {


			List<String> idList=new ArrayList<String>();
		idList.add("asdasd");
		idList.add("asdasd2");
		idList.add("asdasd3");
			System.out.println(StringTools.toOracleStr(idList, true));;



		 		String str = "/test.do?id=$$id$$&name=$NAME1$&name=$name2$&name=$name3$";
		 		Pattern P_ARG = Pattern.compile("(\\$\\w+\\$)", Pattern.CASE_INSENSITIVE);
	}
}
